package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.BankCradInfoBean;
import com.finace.money.data.bean.MoneyRecordBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.adapter.MoneyRecordAdapter;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/28.
 */

public class MoneyRecordActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.recycle_money_out)
    RecyclerView recycleMoneyOut;
    private String mMoneyRecord;
    private ArrayList<MoneyRecordBean> mDatas;
    private MoneyRecordAdapter mMoneyRecordAdapter;

    @Override
    public int setLayout() {
        return R.layout.activity_money_out_record;
    }

    @Override
    public void init() {
        tvHeadTitle.setText(UIUtil.getString(R.string.money_out_record));
        rlLeft.setOnClickListener(this);
        mMoneyRecord = getIntent().getStringExtra(Constant.MONEY_RECORD);
        BankCradInfoBean bankCradInfoBean = (BankCradInfoBean) ACache.get(UIUtil.getContext()).getAsObject(Constant.BANK_CARD_INFO);
        if(null != bankCradInfoBean){
            MessageSendUtil.QryTransferSerial(String.valueOf(bankCradInfoBean.getBankFK()));
        }
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recycleMoneyOut.setLayoutManager(linearLayoutManager);
        mMoneyRecordAdapter = new MoneyRecordAdapter();
        recycleMoneyOut.setAdapter(mMoneyRecordAdapter);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_MONEY_RECORD);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if(mMoneyRecord.equals(Constant.MONEY_IN)){
                mDatas = (ArrayList<MoneyRecordBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.MONEY_IN);
            }else {
                mDatas = (ArrayList<MoneyRecordBean>) ACache.get(UIUtil.getContext()).getAsObject(Constant.MONEY_OUT);
            }
            mMoneyRecordAdapter.setmDatas(mDatas);
            mMoneyRecordAdapter.notifyDataSetChanged();
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_left:
                onBackPressed();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
